Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow unpaginated request #406

Merged
merged 12 commits into from
Dec 13, 2022
Merged

feat: allow unpaginated request #406

merged 12 commits into from
Dec 13, 2022

Conversation

xMase
Copy link
Contributor

@xMase xMase commented Dec 7, 2022

fix #393

@codecov
Copy link

codecov bot commented Dec 7, 2022

Codecov Report

Base: 97.08% // Head: 97.19% // Increases project coverage by +0.11% 🎉

Coverage data is based on head (50652c6) compared to base (a76de9c).
Patch coverage: 100.00% of modified lines in pull request are covered.

❗ Current head 50652c6 differs from pull request most recent head 3da1b75. Consider uploading reports for the commit 3da1b75 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #406      +/-   ##
==========================================
+ Coverage   97.08%   97.19%   +0.11%     
==========================================
  Files           6        7       +1     
  Lines         274      285      +11     
  Branches       92       96       +4     
==========================================
+ Hits          266      277      +11     
  Misses          8        8              
Impacted Files Coverage Δ
src/helper.ts 100.00% <100.00%> (ø)
src/paginate.ts 98.11% <100.00%> (+0.08%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ppetzold
Copy link
Owner

fyi discussion in #393

README.md Outdated
@@ -199,7 +199,7 @@ const paginateConfig: PaginateConfig<CatEntity> {
* Required: false
* Type: number
* Default: 100
* Description: The maximum amount of entities to return per page.
* Description: The maximum amount of entities to return per page. Set it to 0, in conjunction with limit=0 on query param, to disable pagination.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

place addition in next line, as it's a code frame with horizontal slider

src/paginate.ts Outdated
export const DEFAULT_LIMIT = 20
export const NO_PAGINATION = 0

const positiveNumberOrDefault = (value: number | undefined, defaultValue: number, minValue: 0 | 1 = 0) =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to helper.ts

src/paginate.ts Outdated
.skip((page - 1) * limit)
} else {
queryBuilder = repo.take(limit).skip((page - 1) * limit)
if (limit) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use isPaginated

src/paginate.ts Outdated
@@ -362,9 +370,6 @@ export async function paginate<T extends ObjectLiteral>(

;[items, totalItems] = await queryBuilder.getManyAndCount()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this still execute 2 queries ? (1) count and (2) find?

should be then just using getMany(). no need to count if you have no limit.

src/paginate.ts Outdated
} else {
results = {
data: items,
meta: {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just branch the calculation of the meta values which are computed differently now, instead of this larger code duplication

const limit = Math.min(query.limit || config.defaultLimit || 20, config.maxLimit || 100)
const page = positiveNumberOrDefault(query.page, 1, 1)

const defaultLimit = config.defaultLimit || DEFAULT_LIMIT
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, it missed my comment here. here again:

let's untangle this a bit and add isPaginated for readablity

const defaultLimit = config.defaultLimit || DEFAULT_LIMIT
const maxLimit = positiveNumberOrDefault(config.maxLimit, DEFAULT_MAX_LIMIT, 0)
const queryLimit = positiveNumberOrDefault(query.limit, defaultLimit, 0)

const isPaginated = !(queryLimit === NO_PAGINATION && maxLimit === NO_PAGINATION)

const limit = isPaginated ? Math.min(queryLimit, maxLimit || DEFAULT_MAX_LIMIT) : NO_PAGINATION

@ppetzold ppetzold changed the title Allow unpaginated request feat: allow unpaginated request Dec 13, 2022
@ppetzold ppetzold merged commit 0cd2c6a into ppetzold:master Dec 13, 2022
@ppetzold
Copy link
Owner

🎉 This PR is included in version 4.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

FrancYescO pushed a commit to xMase/nestjs-paginate that referenced this pull request Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

disable pagination
2 participants